package com.ruoyi.web.controller.oto;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.PageQueryDTO;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.oto.constants.CompanyTagApplyStatus;
import com.ruoyi.oto.domain.CompanyCore;
import com.ruoyi.oto.domain.CompanyTags;
import com.ruoyi.oto.domain.CompanyTagsCore;
import com.ruoyi.oto.domain.dto.CompanyTagCoreQuryDTO;
import com.ruoyi.oto.domain.dto.SubmitApplyTagValueDTO;
import com.ruoyi.oto.domain.dto.TagsCoreDTO;
import com.ruoyi.oto.domain.vo.CompanyTagCoreQueryParamVO;
import com.ruoyi.oto.service.CompanyCoreService;
import com.ruoyi.oto.service.CompanyTagsCoreService;
import com.ruoyi.oto.service.CompanyTagsService;
import com.ruoyi.oto.tagvisible.TagVisibleRange;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.List;

/**
 * 后台-企业标签管理
 */
@RestController
@RequestMapping("/adm/tag")
public class CompanyTagsController extends BaseController {

    @Autowired
    private CompanyTagsCoreService companyTagsCoreService;

    @Autowired
    private CompanyCoreService companyCoreService;

    @Autowired
    private CompanyTagsService companyTagsService;

    /**
     * 分页查询企业标签库
     * admin账号可以查所有的，其他账号只能查自己的
     * @param quryDTO
     * @return
     */
    @PostMapping("pageTag")
    public AjaxResult<IPage<CompanyTagsCore>> pageCompanyTagCore(@RequestBody CompanyTagCoreQuryDTO quryDTO){
        SysUser user = this.getLoginUser().getUser();
        IPage<CompanyTagsCore> page = new Page<>(quryDTO.getCurrent(),quryDTO.getSize());
        page = companyTagsCoreService.pageTags(user,quryDTO,page);
        return AjaxResult.success(page);
    }

    /**
     * 新增或者修改标签
     * @param saveDTO
     * @return
     */
    @PostMapping("saveTag")
    public AjaxResult<CompanyTagsCore> saveCompanyTagsCore(@RequestBody @Validated TagsCoreDTO saveDTO){
        CompanyTagsCore companyTagsCore  = companyTagsCoreService.saveCompanyTagsCore(saveDTO,this.getLoginUser());
        return AjaxResult.success(companyTagsCore);
    }

    /**
     * 删除标签
     * @param tagIds
     * @return
     */
    @PostMapping("deleteTag")
    public AjaxResult<?> deleteCompanyTagCore(@RequestBody List<Long> tagIds){
        boolean ret = companyTagsCoreService.deleteCompanyTagsCore(tagIds,this.getLoginUser());
        return ret?AjaxResult.success("删除成功"):AjaxResult.error("删除失败");
    }

    /**
     * 获取指定标签的可见范围，
     * @param tagId 如果是新增的话 设置为 0
     * @return
     */
    @GetMapping("getTagVisibleRange/{tagId}")
    public AjaxResult<TagVisibleRange> getTagVisibleRange(@PathVariable("tagId") Long tagId){
        TagVisibleRange  tagVisibleRange =  companyTagsCoreService.getTagVisibleRange(tagId);
        return AjaxResult.success(tagVisibleRange);
    }

    /**
     * 获取待审核的标签
     * @param pageQueryDTO
     * @return
     */
    @PostMapping("listChangedCompanyTags")
    public AjaxResult<IPage<CompanyTags>> listChangedCompanyTags(@RequestBody PageQueryDTO pageQueryDTO){
        IPage<CompanyTags> ret = companyTagsCoreService.listChangedCompanyTags(pageQueryDTO,this.getLoginUser());
        return AjaxResult.success(ret);
    }

    /**
     * 标签提交审核
     * @param submitApplyTagValueDTO
     * @return
     */
    @PostMapping("applyChangedCompanyTags")
    public @ResponseBody AjaxResult<Boolean> applyChangedCompanyTags(@RequestBody SubmitApplyTagValueDTO submitApplyTagValueDTO)
    {
        companyTagsCoreService.applyChangedCompanyTags(submitApplyTagValueDTO,this.getLoginUser());
        return AjaxResult.success(true);
    }

    /**
     * 获取企业的标签列表
     * 当companyId为-1的时候代表获取自己的企业的标签
     * @return
     */
    @GetMapping("companyTagList/{companyId}")
    public AjaxResult<List<CompanyTags>> companyTagList(@PathVariable Long companyId) {
        SysUser sysUser = this.getLoginUser().getUser();
        CompanyCore companyCore = companyCoreService.getById(companyId);
        List<CompanyTags> result= companyTagsService.companyTagListBack(companyCore,sysUser, CompanyTagApplyStatus.审核通过);
        return AjaxResult.success(result);
    }

    /**
     * 获取作为查询参数的标签
     * @return
     */
    @GetMapping("getCompanyTagsQueryParam")
    public AjaxResult<CompanyTagCoreQueryParamVO> getCompanyTagsQueryParam() {
        CompanyTagCoreQueryParamVO result = companyTagsService.getCompanyTagsQueryParam(this.getLoginUser());
        return AjaxResult.success(result);
    }
}
